﻿using System;
using System.Net;
using System.IO;
using System.Xml;
using System.Collections.Generic;

namespace Helpers
{

    public class XMLGate
    {

        public class GateResponse
        {
            public string Result = "";
            public string ResponseData = "";

            public GateResponse(XmlDocument pvXMLResponse)
            {
                try
                {
                    Tools.Logger("XML iNNER: " + pvXMLResponse.InnerText, "info", "GateResponse");
                    Tools.Logger("XML inNER XML: " + pvXMLResponse.InnerXml, "info", "GateResponse");
                    Tools.Logger("XML oUTER: " + pvXMLResponse.OuterXml, "info", "GateResponse");


                    ResponseData = pvXMLResponse.OuterXml;
                    Result = pvXMLResponse.GetElementsByTagName("response")[0].Attributes["result"].Value;
                }
                catch (Exception ex)
                {
                    Tools.Logger("Parse Response Error: " + ex.Message, "error", "GateResponse");
                    Tools.Logger("Parse Response Error Stack: " + ex.StackTrace, "error", "GateResponse");
                    Tools.Logger("Parse Response Error Inner: " + ex.InnerException, "error", "GateResponse");
                }
            }
        }

        public static XmlDocument Request_SEND(XmlDocument pvXMLRequestData, string pvServerUrl, out string pvRequest)
        {
            Tools.Logger("Request_SEND : " + pvServerUrl, "info", "Request_SEND");

            XmlDocument _xmlDocRespone = new XmlDocument();
            pvRequest = "";

            try
            {
                pvRequest = pvXMLRequestData.OuterXml;

                Uri _uri = new Uri(pvServerUrl);
                HttpWebRequest _Request = (HttpWebRequest)WebRequest.Create(_uri);
                _Request.ContentType = "text/xml";
                _Request.Method = "POST";
                _Request.ContentLength = pvXMLRequestData.OuterXml.Length;

                StreamWriter _sw = new StreamWriter(_Request.GetRequestStream());
                _sw.Write(pvXMLRequestData.OuterXml);
                _sw.Flush();
                _sw.Close();

                HttpWebResponse _Respone = (HttpWebResponse)_Request.GetResponse();

                StreamReader _sr = new StreamReader(_Respone.GetResponseStream());
                _xmlDocRespone.LoadXml(_sr.ReadToEnd());
                _sr.Close();
            }
            catch (Exception ex)
            {
                Tools.Logger("Request_SEND Error 1: " + ex.Message, "error", "Request_SEND");
                Tools.Logger("Request_SEND Error 2: " + ex.StackTrace, "error", "Request_SEND");
                Tools.Logger("Request_SEND Error 3: " + ex.InnerException, "error", "Request_SEND");

                _xmlDocRespone = null;
            }

            return _xmlDocRespone;
        }

        public static XmlDocument Request_INIT(BaseTerminalProtocol.Authorizations.Authorization pvAuthor)
        {
            // Init XML Document            
            XmlDocument pvXmlDoc = new XmlDocument();
            XmlDeclaration xmlDeclaration = pvXmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
            pvXmlDoc.AppendChild(xmlDeclaration);

            // Init auth
            XmlElement _xmlEle_auth = XMLHelper.newElement(pvXmlDoc, "auth");
            _xmlEle_auth.Attributes.Append(XMLHelper.newAttribute(pvXmlDoc, "login", pvAuthor.User));
            _xmlEle_auth.Attributes.Append(XMLHelper.newAttribute(pvXmlDoc, "signAlg", "MD5"));
            _xmlEle_auth.Attributes.Append(XMLHelper.newAttribute(pvXmlDoc, "sign", XMLHelper.Encrypt(pvAuthor.Password)));
            // END


            // Init client
            XmlElement _xmlEle_client = XMLHelper.newElement(pvXmlDoc, "client");
            _xmlEle_client.Attributes.Append(XMLHelper.newAttribute(pvXmlDoc, "terminal", pvAuthor.TerminalID));
            _xmlEle_client.Attributes.Append(XMLHelper.newAttribute(pvXmlDoc, "software", "Dealer v0"));
            // END - client

            XmlElement _xmlEle_Root = XMLHelper.newElement(pvXmlDoc, "request");
            _xmlEle_Root.AppendChild(_xmlEle_auth);
            _xmlEle_Root.AppendChild(_xmlEle_client);

            pvXmlDoc.AppendChild(_xmlEle_Root);
            return pvXmlDoc;
        }
     
    }

}